using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMLookupListSortOrderPropsMustExist
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMLookupListSortOrderPropsMustExist


//        [Test]
//        public void Test_CreateDMLookupListSortOrderPropsMustExist_WhenClassNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMLookupListSortOrderPropsMustExist(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//                //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmLookupList", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_CreateDMLookupListSortOrderPropsMustExist()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMLookupListSortOrderPropsMustExist", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
        }

        [Test]
        public void Test_IsValid_True_WhenPropsExists()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            DMClass dmClass = dmLookupList.DMClass;
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMSortOrder order = new DMSortOrder();
            DMSortProperty dmSortProperty = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = property.PropertyName;
            dmLookupList.DMSortOrder = order;
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }
        [Test]
        public void Test_IsValid_True_WhenTwoPropsBothExist()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            DMClass dmClass = dmLookupList.DMClass;
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMProperty property2 = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMSortOrder order = new DMSortOrder();
            DMSortProperty dmSortProperty = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = property.PropertyName;
            DMSortProperty dmSortProperty2 = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty2.PropertyName = property2.PropertyName;
            dmLookupList.DMSortOrder = order;
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dmClass.Properties.Count);
            Assert.AreEqual(2, order.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }
        [Test]
        public void Test_IsValid_False_WhenTwoPropsSecondOneNotExist()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            DMClass dmClass = dmLookupList.DMClass;
            DMProperty property = TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            TestUtilsDMProperty.CreateUnsavedValidDMProperty(dmClass);
            DMSortOrder order = new DMSortOrder();
            DMSortProperty dmSortProperty = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = property.PropertyName;
            DMSortProperty dmSortProperty2 = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty2.PropertyName = "SomeUnknownProp";
            dmLookupList.DMSortOrder = order;
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dmClass.Properties.Count);
            Assert.AreEqual(2, order.DMSortProperties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The LookupList '", rule.Message);
            StringAssert.Contains("'" + dmLookupList.ToString() + "' for property '", rule.Message);
            StringAssert.Contains(" for Business Object '", rule.Message);
            StringAssert.Contains("' cannot have a properties of its sort order that do not exist in the LookupList's Business Object", rule.Message);
        }
        [Test]
        public void Test_IsValid_False_WhenPropsDoesNotExists()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            DMClass dmClass = dmLookupList.DMClass;
            DMSortOrder order = new DMSortOrder();
            DMSortProperty dmSortProperty = order.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = "SomeOtherProperty";
            dmLookupList.DMSortOrder = order;
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClass.Properties.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }
        [Test]
        public void Test_IsValid_True_WhenNoPropsForDMSortOrder()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            dmLookupList.DMSortOrder = new DMSortOrder();
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }
        [Test]
        public void Test_IsValid_True_WhenNoDMSortOrderSet()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenLookupListHasNoDMClass()
        {
            //---------------Set up test pack-------------------
            DMLookupListBusinessObject dmLookupList = TestUtilsDMBusinessObjectLookupList.CreateSavedDMBusinessObjectLookupList();
            dmLookupList.DMClass = null;
            DMSortOrder sortOrder = new DMSortOrder();
            dmLookupList.DMSortOrder = sortOrder;
            DMSortProperty dmSortProperty = sortOrder.DMSortProperties.CreateBusinessObject();
            dmSortProperty.PropertyName = "SomeProperty";
            IBusinessObjectRule rule = new DMLookupListSortOrderPropsMustExist();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmLookupList);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        #endregion
    }
}